package com.salts.controller;

import com.salts.domain.dto.Result;
import com.salts.domain.po.UserAddress;
import com.salts.service.AddressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户地址接口 /address
 */
@RestController
@RequestMapping("/address")
public class UserAddressController {
    private final static Logger logger = LoggerFactory.getLogger(UserAddressController.class);
    @Autowired
    AddressService addressService;

    /**
     * 增删改接口： /insert或/update或/delete -Post
     */
    @PostMapping("/{action}")
    @ResponseBody
    public Result addressCRUD(@PathVariable String action,@RequestBody UserAddress userAddress){
        logger.info("action:" + action + "userAddress:" + userAddress);
        try{
            if (action.equals("insert")){
                addressService.insertUserAddress(userAddress);
            }
            if (action.equals("delete")){
                addressService.deleteUserAddress(userAddress.getId());
            }
            if (action.equals("update")){
                addressService.updateUserAddress(userAddress);
            }
        }catch(Exception e){
            e.printStackTrace(); // 打印异常堆栈信息
            return Result.fail("遇到错误" + e.getMessage());
        }

        return Result.ok("执行成功");
    }

    /**
     * 查询地址接口 /query -Post
     */
    @PostMapping("/query")
    public Result queryUserAddress(@RequestBody Map<String,Integer> queryMap){
        Integer userId = queryMap.get("userId");
        return Result.ok(addressService.selectAllUserAddress(userId));
    }


}
